package impl

import (
	"ylb/config"
	"ylb/entity"
	"ylb/mapper"
	"ylb/vo"
)

type UserServiceImpl struct {
	userMapper mapper.UserMapper
}

func NewUserServiceImpl() UserServiceImpl {
	userServiceImpl := UserServiceImpl{}
	userServiceImpl.userMapper = mapper.NewUserMapper()
	return userServiceImpl

}

func (userService UserServiceImpl) Login(user entity.User) (*entity.User, bool, string, int) {
	userMapper := mapper.NewUserMapper()
	selectUser := userMapper.SelectUserByUserName(user.UserName)

	//用户不存在
	if selectUser.UserName == "" {
		return nil, false, "用户不存在", config.USER_NOT_EXIST_CODE
	}
	//用户密码错误
	if selectUser.Password != user.Password {
		return nil, false, "用户密码错误", config.LOGIN_FAILED_CODE
	}

	return &selectUser, true, "登录成功", config.LOGIN_SUCCESS_CODE

}
func (userService UserServiceImpl) GetUserInfoByUserId(id uint) vo.UserInfoVo {

	user := userService.userMapper.SelectUserById(id)
	relevance := userService.userMapper.SelectUserRelevance(id)

	infoVo := vo.UserParseInfoVo(user, relevance)

	return infoVo

}
func (userService UserServiceImpl) Update(user entity.User) int {

	return int(userService.userMapper.Update(user))

}
func (userService UserServiceImpl) InsertUser(user *entity.User) int64 {
	if user.UserName == "" || user.Password == "" {
		return 0
	}
	return userService.userMapper.InsertOne(user)
}

func (userService UserServiceImpl) SelectUserInfoByUserName(userName string) entity.User {
	return userService.userMapper.SelectUserByUserName(userName)
}
